package com.nettcp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;

public class TCPFileUploadServer {

	public static void main(String[] args) throws IOException {
		ServerSocket serverSocket = new ServerSocket(1314);
		System.out.println("侦听中，端口号1314");
		while(true){
			final Socket socket = serverSocket.accept();
			new Thread(new Runnable() {	
				@Override
				public void run() {
					try {
						InputStream in = socket.getInputStream();
						byte[] buffer = new byte[1024];
						
						File file = new File("D:\\123\\"+Calendar.getInstance().getTimeInMillis());
						if(!file.exists()){
							file.createNewFile();
						}
						FileOutputStream out = new FileOutputStream(file);
						int len=0;
						while( (len = in.read(buffer))!=-1 ){
							out.write(buffer, 0, len);
						}
						out.close();
						in.close();
						System.out.println(socket.getInetAddress().getHostName()+"上传的"+file.getName()+"上传OK！");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					
				}
			}).start();
		}

	}

}
